package com.lazyfish.scss.dao;

import com.lazyfish.scss.bean.User;
import com.lazyfish.scss.util.DBUtil;
import com.lazyfish.scss.util.ResultSetToList;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDAO {
    public User login_check(User user) {
        String sql="SELECT * FROM user WHERE account = ? and password = ?";
        User user2 = new User();
        try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
            ps.setString(1,user.getAccount());
            ps.setString(2,user.getPassword());
            ResultSet rs = ps.executeQuery();
            List<User> list = new ArrayList<User>();
            try {
                list = ResultSetToList.resultSetToList(rs, User.class);
                if(list.size()>0){
                    user2=list.get(0);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user2;
    }
}
